﻿
using System.Collections.Generic;
using System.Data;
using System;
namespace ZHDJ.MobileInterface
{
    public partial class GetMyTaskList : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {
            // 业务逻辑返回的响应对象  
            MyTaskOutParam result = new MyTaskOutParam();
            MyTaskInParam param = new MyTaskInParam();
            string logMsg = "";
            bool isFlag = true;
                 
            try
            {
                param = inParam as MyTaskInParam;
                if (string.IsNullOrEmpty(param.user_id))
                {
                    result.code = "0";
                    result.desc = "用户ID不能为空";

                    isFlag = false;
                }


                if (CommonUtils.IsDisableUsers(param.user_id))
                {
                    result.code = "0";
                    result.desc = "用户状态有问题，请联系管理员";

                    isFlag = false;
                }
                if (param.type != "1" && param.type != "2" && param.type != "3" && param.type != "4")
                {
                    result.code = "0";
                    result.desc = "类别只能为1(三会一课)，2(支部活动)，3(志愿活动)，4(党内关爱)";

                    isFlag = false;
                }

                string userid = param.user_id;
                //if (param.type == "4")
                //{
                //    //只有党务工作者或者组织管理员可以查党内关爱
                //    userid = InterfaceHelper.IsAbleQueryBranchTask(userid);
                //}

                if (param.page_no * param.page_count <= 0)
                {
                    result.code = "0";
                    result.desc = "当前页,以及页记录条数必须为大于0的正整数";

                    isFlag = false;
                } 

                if(isFlag)
                {
                    DataSet ds = InterfaceHelper.GetMyTaskList(userid, param.keyword, param.type, param.page_no, param.page_count);
                    if (ds != null && ds.Tables.Count == 2)
                    {
                        MyTask itemTemp;
                        result.data.total_pages = CommonUtils.GetTotalPages(Convert.ToInt32(ds.Tables[0].Rows[0][0]), param.page_count);
                        foreach (DataRow dr in ds.Tables[1].Rows)
                        {
                            itemTemp = new MyTask();
                            itemTemp.create_time = dr["create_time"].ToString();
                            itemTemp.organiser = dr["organiser"].ToString();
                            itemTemp.rel_id = dr["rel_id"].ToString();
                            itemTemp.title = dr["title"].ToString();
                            itemTemp.todo_id = dr["todo_id"].ToString(); 

                            result.data.list.Add(itemTemp);
                        }
                    }  
                }
                logMsg = result.desc;
            }
            catch (Exception ex)
            {
                result.code = "0";
                result.desc = "操作异常，多次出现请联系管理员"; 
                logMsg = ex.Message + ":" + ex.StackTrace;
            }

            CommonUtils.InsertMobileInterfaceLog(SystemCode +"-GetMyTaskList", SerializeHelper.JsonSerializer<MyTaskInParam>(param), logMsg); 
            return result as OutParam;
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (string.Compare(Request.QueryString["Safe"], "False", true) == 0)
                ProcessRequest<MyTaskInParam, MyTaskOutParam>();
            else
                SafeProcessRequest<MyTaskInParam, MyTaskOutParam>();
        }
    }

    #region 请求参数
    public class MyTaskInParam
    {
        /// <summary>
        /// 必填，	用户ID
        /// </summary>
        public string user_id
        { get; set; }

        /// <summary>
        /// 查询关键字 关键字包括标题、发起人
        /// </summary>
        public string keyword
        { get; set; }

        /// <summary>
        /// 类别 1：三会一课，2：支部活动，3：志愿活动
        /// </summary>
        public string type
        { get; set; }

        /// <summary>
        /// 必填 当前页
        /// </summary>
        public int page_no
        { get; set; }

        /// <summary>
        /// 必填，每页记录条数
        /// </summary>
        public int page_count
        { get; set; }
    }
    #endregion

    #region 结果参数
    public class MyTaskOutParam
    {
        public MyTaskOutParam()
        {
            code = "1";
            desc = "操作成功";
            data = new MyTaskList();
        }

        /// <summary>
        /// 返回结果 具体见接口返回对照码 1	成功 0	失败 2	请求非法 加密结果和传入的MD5字符串进行比较，如果相同则执行接口操作，否则返回1001错误响应码
        /// </summary>
        public string code
        { get; set; }

        /// <summary>
        /// 结果描述 成功和失败都需要
        /// </summary>
        public string desc
        { get; set; }

        /// <summary>
        /// 我的待办事项列表
        /// </summary>
        public MyTaskList data
        { get; set; }
    }

    public class MyTaskList
    {
        public MyTaskList()
        {
            total_pages = 0;
            list = new List<MyTask>();
        }

        public List<MyTask> list { get; set; }

        /// <summary>
        /// 总页数
        /// </summary>
        public int total_pages
        { get; set; }
    }
     
    /// <summary>
    /// 我的待办事项列表
    /// </summary>
    public class MyTask
    {
        /// <summary>
        ///	待办ID	
        /// </summary>
        public string todo_id
        { get; set; }

        /// <summary>
        ///	关系ID	
        /// </summary>
        public string rel_id
        { get; set; }

        /// <summary>
        ///	待办标题	
        /// </summary>
        public string title
        { get; set; }

        /// <summary>
        ///	发起人	
        /// </summary>
        public string organiser
        { get; set; }

        /// <summary>
        ///	创建时间	
        /// </summary>
        public string create_time
        { get; set; } 
    }
    #endregion 
}